package com.mayihr.new_learn_fx.UIControl.treeviewsample;

import com.mayihr.fx.attributeBind.tableViewBind.Account;
import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.binding.ObjectBinding;
import javafx.collections.ListChangeListener;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.CheckBoxTreeItem;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.CheckBoxTreeCell;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.util.ArrayList;
import java.util.List;

public class TreeViewSample5 extends Application {
    private List<Account> accounts;

    {
        accounts = new ArrayList<>();
        accounts.add(new Account("Tom1", 1));
        accounts.add(new Account("Tom2", 2));
        accounts.add(new Account("Tom3", 3));
        accounts.add(new Account("Tom4", 4));
        accounts.add(new Account("Tom5", 5));
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        primaryStage.setTitle("Tree View Sample");

        CheckBoxTreeItem<String> rootItem =
                new CheckBoxTreeItem<>("View Source Files");
        rootItem.setExpanded(true);

        final TreeView tree = new TreeView(rootItem);
        tree.setEditable(true);

        tree.setCellFactory(CheckBoxTreeCell.forTreeView());
//        tree.setCellFactory(CheckBoxTreeCell.forTreeView());
//        for (int i = 0; i < 8; i++) {
        for (Account account : accounts) {
            final CheckBoxTreeItem<String> checkBoxTreeItem =
                    new CheckBoxTreeItem<>(account.getName());
            rootItem.getChildren().add(checkBoxTreeItem);

//            checkBoxTreeItem.selectedProperty().addListener((x, y, z) -> {
//                System.out.println("bbb");
//                if (checkBoxTreeItem.isSelected()) {
//                    System.out.println(account.toString());
//                }
//            });
//            checkBoxTreeItem.selectedProperty().addListener(o -> {
//                System.out.println("aaa");
//                if (checkBoxTreeItem.isSelected()) {
//                    System.out.println(account.toString());
////                    Account a = new Account("jerry", 100);
////                    accounts.add(a);
////                    CheckBoxTreeItem<String> checkBoxTree =
////                            new CheckBoxTreeItem<>(a.getName());
////                    rootItem.getChildren().add(checkBoxTree);
//                }
//            });
            EventHandler<MouseEvent> mouseEventHandle = (MouseEvent event) -> {
                System.out.println(account.toString() + 1);
            };
            checkBoxTreeItem.addEventHandler(MouseEvent.MOUSE_CLICKED, mouseEventHandle);
        }

//        }

        tree.setRoot(rootItem);
        tree.setShowRoot(true);

        StackPane root = new StackPane();
        root.getChildren().add(tree);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
